/**
 * 创建人:  @author huangpeng    
 * 创建时间:  2022年09月25日 17:12
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  CommentServiceImpl
 * 文件描述:  @Description: 评论信息service实现类
 * 公司名称:  柠檬
 *
 * All rights Reserved, Designed By 柠檬 
 * @Copyright: 2022-2022
 *
 */
package com.lemon.blog.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lemon.blog.web.domain.BlComment;
import com.lemon.blog.web.mapper.BlCommentMapper;
import com.lemon.blog.web.mapper.BlCommentOptionsMapper;
import com.lemon.blog.web.service.BlCommentService;
import com.lemon.common.core.utils.StringUtils;
import com.lemon.common.security.service.BaseServiceImpl;
import com.lemon.common.security.utils.OptEntityUtils;
import com.lemon.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 包名称：com.lemon.blog.service.impl
 * 类名称：CommentServiceImpl
 * 类描述：评论信息service实现类
 * 创建人：@author huangpeng
 * 创建时间：2022年09月25日 17:12
 */
@Service
public class BlCommentServiceImpl extends BaseServiceImpl<BlComment> implements BlCommentService {

    public static final Logger logger = LoggerFactory.getLogger(BlCommentServiceImpl.class);

    @Autowired
    private BlCommentMapper commentMapper;

    @Autowired
    private BlCommentOptionsMapper blCommentOptionsMapper;

    @Override
    public List<BlComment> selectCommentList(BlComment model) {
        if (StrUtil.isNotEmpty(SecurityUtils.getUsername())) {
            model.setCurrUserId(SecurityUtils.getLoginUser().getUserid());
        } else {
            model.setCurrUserId(null);
        }
        List<BlComment> commentList = commentMapper.selectCommentList(model);
        return buildCommentTree(commentList);
    }

    @Override
    public int insertByBatch(List<BlComment> commentList) {
        for (BlComment comment : commentList) {
            comment.setUserId(SecurityUtils.getUserId().toString());
            OptEntityUtils.preInsert(comment);
        }
        return commentMapper.batchInsert(commentList);
    }

    @Override
    public List<BlComment> listMyComment(BlComment comment) {
        if (StrUtil.isNotEmpty(SecurityUtils.getUsername())) {
            comment.setCurrUserId(SecurityUtils.getLoginUser().getUserid());
        } else {
            comment.setCurrUserId(null);
        }
        return commentMapper.selectCommentList(comment);
    }

    @Override
    public List<BlComment> listAboutComment(BlComment model) {
        if (StrUtil.isNotEmpty(SecurityUtils.getUsername())) {
            model.setCurrUserId(SecurityUtils.getLoginUser().getUserid());
        } else {
            model.setCurrUserId(null);
        }
        List<BlComment> commentList = commentMapper.listAboutComment(model);
        return buildCommentTree(commentList);
    }

    /**
     * 获取评论树
     * @Title: buildCommentTree
     * @Description: 获取评论树
     * @param commentList
     * @return: List<BlComment>
     * @throws:
     * @author: huangpeng
     * @Date: 2022/11/26 22:18
     */
    private List<BlComment> buildCommentTree(List<BlComment> commentList) {
        List<BlComment> resultList = new ArrayList<>();
        List<Long> tempList = new ArrayList<>();
        for (BlComment comment : commentList) {
            tempList.add(comment.getId());
        }
        for (BlComment comment : commentList) {
            // 如果是顶级节点，则遍历该父节点的所以子节点
            if (StringUtils.isEmpty(comment.getToCommentId()) || (
                    StringUtils.isNotEmpty(comment.getToCommentId()) && !tempList.contains(Long.parseLong(comment.getToCommentId()))
                )) {
                recursionData(commentList, comment);
                resultList.add(comment);
            }
        }
        if (resultList.isEmpty()) {
            resultList = commentList;
        }
        return resultList;
    }

    /**
     * 递归列表
     * @Title: recusionFn
     * @Description: 递归列表
     * @param commentList
     * @param comment
     * @return: void
     * @throws:
     * @author: huangpeng
     * @Date: 2022/11/26 22:21
     */
    private void recursionData(List<BlComment> commentList, BlComment comment) {
        // 得到子节点列表
        List<BlComment> childList = getChildList(commentList, comment);
        comment.setChildCommentList(childList);
        for (BlComment cChild : childList) {
            if (hasChild(commentList, cChild)) {
                recursionData(commentList, cChild);
            }
        }
    }

    /**
     * 得到子节点列表
     * @Title: getChildList
     * @Description: 得到子节点列表
     * @param commentList
     * @param comment
     * @return: List<BlComment>
     * @throws:
     * @author: huangpeng
     * @Date: 2022/11/26 22:23
     */
    private List<BlComment> getChildList(List<BlComment> commentList, BlComment comment) {
        List<BlComment> childList = new ArrayList<>();
        for (BlComment child : commentList) {
            if (StringUtils.isNotNull(child.getToCommentId()) && child.getToCommentId().equals(comment.getId().toString())) {
                childList.add(child);
            }
        }
        return childList;
    }

    /**
     * 判断是否有子节点
     * @Title: hasChild
     * @Description: 判断是否有子节点
     * @param commentList
     * @param cChild
     * @return: boolean
     * @throws:
     * @author: huangpeng
     * @Date: 2022/11/26 22:31
     */
    private boolean hasChild(List<BlComment> commentList, BlComment cChild) {
        return getChildList(commentList, cChild).size() > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteByBatch(Long[] id) {
        List<Long> idList = new ArrayList<>();
        // 获取当前评论的子评论
        BlComment comment = commentMapper.selectByPrimaryKey(id[0]);
        getChildCommentList(comment, idList);
        // 删除博客相关信息
        blCommentOptionsMapper.deleteByCommentIdsAndUserId(SecurityUtils.getUserId(), idList.toArray(new Long[0]));
        return commentMapper.deleteByBatch(idList.toArray(new Long[0]));
    }

    /**
     * @Title: getChildCommentList
     * @Description: 获取评论的子评论
     * @param idList
     * @return: List<BlComment>
     * @throws:
     * @author: huangpeng
     * @Date: 2023/8/18 18:41
     */
    private void getChildCommentList(BlComment comment, List<Long> idList) {
        List<BlComment> modelList = commentMapper.selectChildListById(comment.getId());
        if (modelList.size() > 0) {
            for (BlComment blComment : modelList) {
                getChildCommentList(blComment, idList);
            }
        }
        idList.add(comment.getId());
    }
}
